package com.xzzz.backend.bsms.server.rbac.sys.dict.name;

import cn.hutool.core.annotation.AnnotationUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.xzzz.common.base.util.spring.SpringUtil;
import org.springframework.util.ObjectUtils;

/**
 * 用于 EasyExcel 中的字段转换
 */
public class DictConverter implements Converter<Object> {

    @Override
    public Class<Object> supportJavaTypeKey() {
        return Object.class;
    }

    @Override
    public WriteCellData<String> convertToExcelData(Object object, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
        if (ObjectUtils.isEmpty(object)) {
            return new WriteCellData<>("");
        }
        DictName dict = AnnotationUtil.getAnnotation(contentProperty.getField(), DictName.class);
        String groupId = StrUtil.isBlank(dict.value()) ? dict.groupId() : dict.value();
        String dictName = SpringUtil.getBean(DictCache.class).getDictName(groupId, object.toString());
        return new WriteCellData<>(dictName);
    }
}
